import numpy as np
import seaborn as sns
import warnings 
from scipy import stats
from scipy.stats import norm
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.metrics import mean_squared_error
from sklearn.cross_validation import train_test_split
from sklearn.decomposition import pca
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import MinMaxScaler

color = sns.color_palette()#sns调色板
sns.set_style('dark')
warnings.filterwarnings('ignore')


#数据获取
df = pd.read_table('zhengqi_train.txt')
df_test = pd.read_table('zhengqi_test.txt')

#找出相关程度
plt.figure(figsize=(20,16))#指定绘图对象的宽度和高度
column = df.columns.tolist()[:39]#列表头
mcorr = df[column].corr()#相关系数矩阵，即给出了任意两个变量之间的相关系数
mask = np.zeros_like(mcorr,dtype=np.bool)#构造和mcorr同维度的矩阵，为bool型
mask[np.triu_indices_from(mask)] = True# 角平分线右侧为True
cmap = sns.diverging_palette(220,10,as_cmap=True)# 返回matplotlib colormap对象
g = sns.heatmap(mcorr,mask=mask,cmap=cmap,square=True,annot=True,fmt='0.2f')#热力图（两两相似度）
plt.show()

column_drop = [c for c in mcorr['target'].index if abs(mcorr['target'][c])<0.15]
df = df.drop(column_drop,axis=1)
df_test = df_test.drop(column_drop,axis=1)

#模型训练
clf = LinearRegression()
x = df.values[:,0:-1]
x_test = df_test.values
y = df.values[:,-1]
pca = pca.PCA(n_components=0.95)
pca.fit(x)
x_pca = pca.transform(x)
x_test_pca = pca.transform(x_test)
#mm = MinMaxScaler()
#mm.fit(x)
#x = mm.transform(x)
#x_test = mm.transform(x_test)
#x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.2,random_state=2)
#clf.fit(x_train,y_train)
#pred = clf.predict(x_test)
#print(mean_squared_error(pred,y_test))
clf.fit(x_pca,y)
pred = clf.predict(x_test_pca)
pd.Series(pred).to_csv('pred_2018.12.10.txt',index=False)


